package com.zhoug.zmui.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;

/**
 * 地图sdk工具
 *
 * @Author 35574
 * @Date 2020/8/3
 * @Description
 */
public class ZMUIMapSdkManager {
    private static final String TAG = "ZMUIMapSdkManager";
    private static  boolean debug = false;
    /**
     * 百度地图包名
     */
    public static final String PACKAGE_MAP_BAIDU = "com.baidu.BaiduMap";
    /**
     * 高德地图包名
     */
    public static final String PACKAGE_MAP_GAODE = "com.autonavi.minimap";
    /**
     * 腾讯地图包名
     */
    public static final String PACKAGE_MAP_TENCENT = "com.tencent.map";



    private String coordType = "bd09ll";
    private double targetLatitude;//终点维度
    private double targetLongitude;//终点经度
    private String targetAddress;//终点地址
    private String targetName;//终点名称
    private Context context;


    public static void setDebug(boolean debug) {
        ZMUIMapSdkManager.debug = debug;
    }

    public ZMUIMapSdkManager(Context context) {
        this.context = context;
    }


    /**
     * 调用百度地图路线规划
     * 公交、驾车、步行、骑行路线规划
     */
    public void toBaiduMapRoutePlanning() {
        if (AppUtils.isAppInstalled(PACKAGE_MAP_BAIDU)) {
            String uriString = "baidumap://map/direction?";
            boolean or=false;
            if (targetLatitude != 0 && targetLongitude != 0) {
                uriString += "destination=latlng:" + targetLatitude+ "," + targetLongitude;
                or=true;
            }
            if (!StringUtils.isNullOrEmpty(targetName)) {
                if(or){
                    uriString+="|";
                }
                uriString += "name:" + targetName;
                or=true;
            }
            if (!StringUtils.isNullOrEmpty(targetAddress)) {
                if(or){
                    uriString+="|";
                }
                uriString += "addr:" + targetAddress;
            }

            uriString += "&coord_type=" + coordType
                    + "&mode=driving"
                    + "&src=" + context.getPackageName();
            LogUtils.d(TAG, "toBaiduMap: uriString=" + uriString);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addCategory("android.intent.category.DEFAULT");
            Uri uri = Uri.parse(uriString);
            intent.setData(uri);
            context.startActivity(intent); // 启动调用
        } else {
            Toast.makeText(context, "请先安装百度地图客户端", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 调用高德地图路线规划
     * 公交、驾车、步行、骑行路线规划
     */
    public void toGaodeMapRoutePlanning() {
        if (AppUtils.isAppInstalled(PACKAGE_MAP_GAODE)) {
            String uriString = "amapuri://route/plan/?";
            if (targetLatitude != 0 && targetLongitude != 0) {
                uriString += "dlat=" + targetLatitude + "&dlon=" + targetLongitude;
                if (targetName != null) {
                    uriString += "&dname" + targetName;
                }
            } else {
                if (targetName != null) {
                    uriString += "dname" + targetName;
                }
            }


            uriString += "&dev=0" + "&t=0" + "&sourceApplication=" + context.getPackageName();
            LogUtils.d(TAG, "toGaodeMapRoutePlanning: uriString=" + uriString);

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setPackage(PACKAGE_MAP_GAODE);
            intent.addCategory("android.intent.category.DEFAULT");
            Uri uri = Uri.parse(uriString);
            intent.setData(uri);
            context.startActivity(intent); // 启动调用

        } else {
            Toast.makeText(context, "请先安装高德地图客户端", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 调用腾讯地图路线规划
     * 公交、驾车、步行、骑行路线规划
     */
    public void toTencentMapRoutePlanning() {
        if (AppUtils.isAppInstalled(PACKAGE_MAP_TENCENT)) {
            String uriString = "qqmap://map/routeplan?";
            uriString += "type=drive";
            if (targetLatitude != 0 && targetLongitude != 0) {
                uriString += "&tocoord=" + targetLatitude + "," + targetLongitude + "&to=" + targetName;
            } else {
                uriString += "to=" + targetName;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addCategory("android.intent.category.DEFAULT");
            Uri uri = Uri.parse(uriString);
            intent.setData(uri);
            context.startActivity(intent); // 启动调用

        } else {
            Toast.makeText(context, "请先安装腾讯地图客户端", Toast.LENGTH_SHORT).show();
        }
    }





    /**
     * 设置终点经纬度
     *
     * @param targetLatitude  终点维度
     * @param targetLongitude 终点经度
     */
    public void setTargetLocation(double targetLatitude, double targetLongitude) {
        this.targetLatitude = targetLatitude;
        this.targetLongitude = targetLongitude;
    }

    /**
     * 地址终点地名
     *
     * @param targetName
     */
    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }

    /**
     * 设置终点地址
     *
     * @param targetAddress
     */
    public void setTargetAddress(String targetAddress) {
        this.targetAddress = targetAddress;
    }

    /**
     * 设置百度经纬度编码
     *
     * @param coordType
     */
    public void setCoordType(String coordType) {
        this.coordType = coordType;
    }

    public String getCoordType() {
        return coordType;
    }


}
